package com.aviation.sell_ticket.controller;

import cn.hutool.core.lang.Validator;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aviation.sell_ticket.service.FlightService;
import com.aviation.sell_ticket.tool.ResultEnum;
import com.aviation.sell_ticket.tool.ResultUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("FlightAction")
public class FlightController {

    @Resource
    private FlightService flightService;

    @PostMapping("oneWayQuery")
    public Map<String,Object> oneWayQuery(String departure, String destination, String departureTime){

        try {
            if (Validator.isNull(departure)) {
                return ResultUtil.success(ResultEnum.MISSING_QUERY_INFORMATION, null);
            }else if(Validator.isNull(destination)){
                return ResultUtil.success(ResultEnum.MISSING_QUERY_INFORMATION, null);
            }else if(Validator.isNull(departureTime)){
                return ResultUtil.success(ResultEnum.MISSING_QUERY_INFORMATION, null);
            }
            //转换时间
            DateTimeFormatter struct = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate localDate = LocalDate.parse(departureTime,struct);

            //判断时间是否是今天或今后
            if(!localDate.isAfter(LocalDate.now()) && !localDate.isEqual(LocalDate.now())){
                return ResultUtil.success(ResultEnum.THE_TIME_SHOULD_BE_TODAY_OR_LATER, null);
            }

            return flightService.oneWayQuery(departure,destination, localDate);
        }catch (Exception e){
            return ResultUtil.beDefeated(ResultEnum.UNKNOWN_ERROR);
        }

    }

    @PostMapping("transferQuery")
    public Map<String,Object> transferQuery(String departure, String destination, String departureTime){

        try {
            if (Validator.isNull(departure)) {
                return ResultUtil.success(ResultEnum.MISSING_QUERY_INFORMATION, null);
            }else if(Validator.isNull(destination)){
                return ResultUtil.success(ResultEnum.MISSING_QUERY_INFORMATION, null);
            }else if(Validator.isNull(departureTime)){
                return ResultUtil.success(ResultEnum.MISSING_QUERY_INFORMATION, null);
            }
            //转换时间
            DateTimeFormatter struct = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate localDate = LocalDate.parse(departureTime,struct);

            //判断时间是否是今天或今后
            if(!localDate.isAfter(LocalDate.now()) && !localDate.isEqual(LocalDate.now())){
                return ResultUtil.success(ResultEnum.THE_TIME_SHOULD_BE_TODAY_OR_LATER, null);
            }

            return flightService.transferQuery(departure,destination, localDate);
        }catch (Exception e){
            System.out.println(e);
            return ResultUtil.beDefeated(ResultEnum.UNKNOWN_ERROR);
        }

    }
    
    @PostMapping("flightMap")
    public Map<String, Object> flightMap(@RequestBody JSONObject jsonObject){

        try {
            //提取数据
            String departure = JSON.toJSONString(jsonObject.get("departure")).replace("\"","");
            String start = JSON.toJSONString(jsonObject.get("start")).replace("\"","");
            String end = JSON.toJSONString(jsonObject.get("end")).replace("\"","");
            JSONArray destination = JSON.parseArray(JSON.toJSONString(jsonObject.get("destination")));

            //验证数据完整
            if(Validator.isNull(departure) || Validator.isNull(start) || Validator.isNull(end)){
                return ResultUtil.success(ResultEnum.MISSING_QUERY_INFORMATION, null);
            }else if(!Validator.isNull(destination)){
                List<String> destinationCity=new ArrayList<>();
                for (Object o : destination) {
                    destinationCity.add(String.valueOf(o));
                }

                //验证时间是否正确
                DateTimeFormatter struct = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDate startTime=LocalDate.parse(start,struct);
                LocalDate endTime=LocalDate.parse(end,struct);
                LocalDate time=LocalDate.now();
                if(time.isBefore(startTime) || time.equals(startTime)){
                    if(time.isBefore(endTime)){
                        if(endTime.isAfter(startTime)){
                            //取出数据
                            return flightService.flightMap(departure,destinationCity,startTime,endTime);
                        }else {
                            return ResultUtil.success(ResultEnum.THE_END_TIME_SHOULD_BE_BEFORE_THE_DEPARTURE_TIME, null);
                        }
                    }else {
                        return ResultUtil.success(ResultEnum.THE_TIME_SHOULD_BE_TODAY_OR_LATER, null);
                    }
                }else{
                    return ResultUtil.success(ResultEnum.THE_TIME_SHOULD_BE_TODAY_OR_LATER, null);
                }
            }
            return ResultUtil.success(ResultEnum.MISSING_QUERY_INFORMATION, null);
        }catch (Exception e){
            return ResultUtil.beDefeated(ResultEnum.UNKNOWN_ERROR);
        }
    }
}
